<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2010 Nitin Sewtahalsing <nitin@refresj.nl>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Plugin 'Social Network share for tt_news' for the 'ttnews_share' extension.
 *
 * @author	Nitin Sewtahalsing <nitin@refresj.nl>
 * @package	TYPO3
 * @subpackage	tx_ttnewsshare
 */
class tx_ttnewsshare_pi1 extends tslib_pibase {
	var $prefixId      = 'tx_ttnewsshare_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_ttnewsshare_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'ttnews_share';	// The extension key.
	var $pi_checkCHash = true;
	
	/**
	 * The extraItemMarkerProcessor function from tt_news
	 * 
	 * @return	Social Network share icons into tt_news marker
	 */
	function extraItemMarkerProcessor($markerArray, $row, $conf, &$pObj) {
		$this->pObj = $pObj;
		$this->conf = $conf;
		$this->row = $row;
		$this->baseurl = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
		$this->extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['ttnews_share']);
		
		if(empty($GLOBALS['TSFE']->ttnews_share)){
			$GLOBALS['TSFE']->ttnews_share = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*','tx_ttnewsshare_networks','deleted="0" AND hidden="0"','','sorting ASC');
		}
		
		if(isset($_GET['tx_ttnews']['tt_news'])) $markerArray['###SOCIAL_NETWORKS###'] = $this->getSocialNetworks();
		else  $markerArray['###SOCIAL_NETWORKS###']='';
		
		return $markerArray;
	}
	
	
	
	
	
	/**
	 * Make Social Network share links
	 *
	 * @return	$content	STRING	Social Network share links
	 */
	function getSocialNetworks(){
		$content = '';
		
		$title = rawurlencode(html_entity_decode($this->row['title']));
		$text = rawurlencode(substr(html_entity_decode(strip_tags($this->row['bodytext'])),0,255).'...');
		$conf = array(
			'parameter' => ($this->conf['socialNetworkShare.']['singlePid'] != null ? $this->conf['socialNetworkShare.']['singlePid'] : $GLOBALS['TSFE']->id),
			'additionalParams' => '&tx_ttnews[tt_news]='.$this->row['uid'],
			'useCacheHash' => true,
			'returnLast' => 'url',
		);
		$url_news = $this->pObj->cObj->typoLink('', $conf);
		
		foreach($GLOBALS['TSFE']->ttnews_share as $sn)
		{
			$permalink = rawurlencode($this->baseurl.$url_news);
			
			if($sn['shorturl'] == 1)
			{
				$permalink = $this->row['tx_ttnewsshare_short_url'];
				
				if(empty($permalink) && $this->extConf['APIkey']!='')
				{
					$permalink = $this->getShortURL($this->baseurl.$url_news);
				}
			}
		
			$link = str_replace('###LINK###',$permalink,$sn['url']);
			$link = str_replace('###TITLE###',$title,$link);
			$link = str_replace('###TEXT###',$text,$link);
			$link = str_replace('###BLOGNAME###',$this->pObj->conf['socialNetworkShare.']['blogName'],$link);
			
			$content .= '<a href="'.$link.'" title="'.$sn['title'].'" target="_blank" class="share_icon" rel="nofollow"><img src="'.$this->baseurl.$sn['imagepath'].'" alt="'.$sn['title'].'"></a>';
		}
		
		return $content;
	}
	
	
	
	
	/**
	 * Make the bit.ly short URL
	 * 
	 * @param	$permalink	String	The long URL
	 * 
	 * @return	$short_url	String	The shortend URL
	 */
	function getShortURL($permalink){
		$api_url = 'http://api.bit.ly/v3/shorten?login='.$this->extConf['username'].'&apiKey='.$this->extConf['APIkey'].'&uri='.$permalink;
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_URL, $api_url);
		$res = curl_exec($curl);
		$result = json_decode($res);
		curl_close($curl);
		
		if($result->status_code == 200){
			$update = array('tx_ttnewsshare_short_url' => $result->data->url);
			$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_news','uid = "'.$this->row['uid'].'"',$update);
			return $result->data->url;
		} else {
			return $permalink;
		}
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/ttnews_share/pi1/class.tx_ttnewsshare_pi1.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/ttnews_share/pi1/class.tx_ttnewsshare_pi1.php']);
}

?>